package com.mes.controller.manu;

import com.mes.service.manu.FlyWeavService;
import com.interf.agv.AGVUtil;
import com.yy.base.page.Message;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 飞织作业
 */
@Controller
@RequestMapping("flyWeavController")
public class FlyWeavController {
    @Autowired
    private FlyWeavService flyWeavService;
    private static JSONObject json = null;

    //加载飞织作业单
    @ResponseBody
    @RequestMapping(value="selectFlyWeavList",method= RequestMethod.POST)
    public Message selectFlyWeavList(HttpServletRequest request){
        Message msg = new Message();
        Map map = new HashMap<String,String>();
        map.put("flyWeav", request.getParameter("flyWeav"));
        map.put("status", request.getParameter("status"));
        map.put("jobCode", request.getParameter("jobCode"));

        msg.setSplitPageList(flyWeavService.selectFlyWeavList(map));

        return msg;
    }
    //加载飞织机台
    @ResponseBody
    @RequestMapping(value="selectFlyWeavs",method= RequestMethod.POST)
    public Message selectFlyWeavs(HttpServletRequest request){
        Message msg = new Message();
        msg.setData("flyWeavs",flyWeavService.selectFlyWeavs());
        return msg;
    }
    //加载工单状态
    @ResponseBody
    @RequestMapping(value="selectJobStatus",method= RequestMethod.POST)
    public Message selectJobStatus(HttpServletRequest request){
        Message msg = new Message();

        msg.setData("jobStatus",flyWeavService.selectJobStatus());
        return msg;
    }
    //修改工单是否录入辅料RFID状态
    @ResponseBody
    @RequestMapping(value="updateJobStatus",method= RequestMethod.POST)
    public Message updateJobStatus(HttpServletRequest request){
        String jobCode  = request.getParameter("jobCode");
        String position  = request.getParameter("POSITION");
        flyWeavService.UpdateJobMaterialok(jobCode);
        String fzj  = request.getParameter("fzj");

        //调用AGV到写RFID位置
        String [] ps = position.split(",");
        String jsonStr= "{\"task_seq\":\"" + jobCode + "\",\"submd\":1,\"flyloom_id\":" + fzj + "," +
                "\"dest_position\":{\"x\":" + ps[0] + ",\"y\":" + ps[1] + "}" +
                "}";
        return AGVUtil.sendAGVTask(jsonStr, 0x00060001);
    }

    /**
     * 调用agv接口取鞋面
     */
    @ResponseBody
    @RequestMapping(value="callAgv",method= RequestMethod.POST)
    public Message callAgv(HttpServletRequest request) {
        String position  = request.getParameter("POSITION");
        String code = request.getParameter("code");
        //修改鞋面状态为取料中
        flyWeavService.updateXmStatus(code, "GET");
        String fzj  = request.getParameter("fzj");

        String [] ps = position.split(",");
        String jsonStr= "{\"task_seq\":\"" + code + "\",\"submd\":3,\"flyloom_id\":" + fzj + "," +
                "\"dest_position\":{\"x\":" + ps[0] + ",\"y\":" + ps[1] + "}" +
                "}";
        return AGVUtil.sendAGVTask(jsonStr, 0x00060003);
    }
    /**
     * 标记agv接口取鞋面完成
     */
    @ResponseBody
    @RequestMapping(value="setAgvEnd",method= RequestMethod.POST)
    public Message setAgvEnd(HttpServletRequest request) {
        String position  = request.getParameter("POSITION");
        String code = request.getParameter("code");
        //修改鞋面状态为完成
        flyWeavService.updateXmStatus(code, "OK");
        String fzj  = request.getParameter("fzj");

        String [] ps = position.split(",");
        String jsonStr= "{\"task_seq\":\"" + code + "\",\"submd\":5,\"flyloom_id\":" + fzj + "," +
                "\"dest_position\":{\"x\":" + ps[0] + ",\"y\":" + ps[1] + "}" +
                "}";
        return AGVUtil.sendAGVTask(jsonStr, 0x00060005);
    }
}
